package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.MainPagerAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.bean.CreativeProductBean;
import com.dinadi.yidi.ui.fragment.ComprehensiveCommodityFragment;
import com.dinadi.yidi.ui.fragment.ComprehensiveCommodityFragment2;
import com.dinadi.yidi.ui.fragment.ComprehensiveCommodityFragment3;
import com.dinadi.yidi.ui.view.DatePopup;
import com.dinadi.yidi.ui.view.NoScrollViewPager;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * author: JiaYu.He
 * Date: 2018/3/7   商品分类
 * Time: 18:17
 */

public class ProductClassificationActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private TextView tv_confirm, tv_ascending, tv_descending;
    private EditText et_lower_price, et_higher_price;
    private RelativeLayout layout_empty, bg_ascending, bg_descending;
    private LinearLayout title_layout;
    private RadioGroup rg_main;
    private Drawable drawable_price, drawable_degree_of_heate;
    private RadioButton rb_comprehensive, rb_price, rb_degree_of_heate;
    private List<BaseFragment> fragments = new ArrayList<>();
    private List<RadioButton> radioButtons = new ArrayList<>();
    private DatePopup popup;
    private CreativeProductBean bean;
    private String title, categoryId;
    private int heat_temperature = 0;//0,热度升序   1，降序
    private int price_temperature = 0;//0,价格升序   1，降序
    private int lower;
    private int higher;
    private String lower_price, higher_price;
    private int mIndex;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(ProductClassificationActivity.this).inflate(R.layout.activity_product_classification, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {

        changePager(2);
        title_layout = (LinearLayout) view.findViewById(R.id.title_layout);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        rb_comprehensive = (RadioButton) view.findViewById(R.id.rb_comprehensive);
        rb_price = (RadioButton) view.findViewById(R.id.rb_price);
        rb_degree_of_heate = (RadioButton) view.findViewById(R.id.rb_degree_of_heate);
        rg_main = (RadioGroup) view.findViewById(R.id.rg_main);
        bean = new CreativeProductBean();
        changeImageSize();
    }

    private void changeImageSize() {
        //定义底部标签图片大小
        drawable_price = getResources().getDrawable(R.drawable.icon_default);
        drawable_price.setBounds(0, 0, 30, 50);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        rb_price.setCompoundDrawables(null, null, drawable_price, null);//只放上面

        drawable_degree_of_heate = getResources().getDrawable(R.drawable.icon_default);
        drawable_degree_of_heate.setBounds(0, 0, 30, 50);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        rb_degree_of_heate.setCompoundDrawables(null, null, drawable_degree_of_heate, null);//只放上面

    }

    @Override
    protected void initListener() {
        rb_comprehensive.setOnClickListener(this);
        rb_price.setOnClickListener(this);
        rb_degree_of_heate.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        categoryId = intent.getStringExtra("categoryId");
        tvTitle.setText(title);
        fragments.add(new ComprehensiveCommodityFragment());
        fragments.add(new ComprehensiveCommodityFragment2());
        fragments.add(new ComprehensiveCommodityFragment3());
        radioButtons.add(rb_comprehensive);
        radioButtons.add(rb_price);
        radioButtons.add(rb_degree_of_heate);
        //开启事务
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //添加首页
        ft.add(R.id.content, new ComprehensiveCommodityFragment()).commit();

        //默认设置为第0个
        setIndexSelected(0);
        selectRadioButton(0);

    }

    private void setIndexSelected(int index) {

        if (mIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();


        //隐藏
        ft.hide(fragments.get(mIndex));
        //判断是否添加
        if (!fragments.get(index).isAdded()) {
            ft.add(R.id.content, fragments.get(index)).show(fragments.get(index));
        } else {
            ft.show(fragments.get(index));
        }

        ft.commit();
        //再次赋值
        mIndex = index;

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.rb_comprehensive:
                setIndexSelected(0);
                selectRadioButton(0);
                changeImageSize();
                break;
            case R.id.rb_price:
                if (popup != null) {
                    if (popup.isShowing()) {
                        popup.dismiss();
                        popup = null;
                    } else {
                        showPopup();
                    }
                } else {
                     /*弹框*/
                    showPopup();
                }
                break;
            case R.id.rb_degree_of_heate:
                setIndexSelected(2);
                selectRadioButton(2);
                drawable_price = getResources().getDrawable(R.drawable.icon_default);
                drawable_price.setBounds(0, 0, 30, 50);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
                rb_price.setCompoundDrawables(null, null, drawable_price, null);//只放上面
                if (heat_temperature == 0) {
                    heat_temperature = 1;
                    bean.setSortType("2");
                    bean.setCategoryId(categoryId);
                    EventBus.getDefault().post(bean);
                    drawable_degree_of_heate = getResources().getDrawable(R.drawable.icon_top);
                    drawable_degree_of_heate.setBounds(0, 0, 30, 50);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
                    rb_degree_of_heate.setCompoundDrawables(null, null, drawable_degree_of_heate, null);//只放上面
                } else {
                    heat_temperature = 0;
                    bean.setSortType("3");
                    bean.setCategoryId(categoryId);
                    EventBus.getDefault().post(bean);
                    drawable_degree_of_heate = getResources().getDrawable(R.drawable.icon_bottom);
                    drawable_degree_of_heate.setBounds(0, 0, 30, 50);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
                    rb_degree_of_heate.setCompoundDrawables(null, null, drawable_degree_of_heate, null);//只放上面
                }
                break;
        }

    }

     /*价格弹框*/


  /*价格弹框*/

    private void showPopup() {
        View view = View.inflate(ProductClassificationActivity.this, R.layout.popupwindow_price_sort, null);
        final View view_pellucidity = view.findViewById(R.id.view_pellucidity);
        layout_empty = (RelativeLayout) view.findViewById(R.id.layout_empty);
        tv_confirm = (TextView) view.findViewById(R.id.tv_confirm);
        bg_ascending = (RelativeLayout) view.findViewById(R.id.bg_ascending);
        bg_descending = (RelativeLayout) view.findViewById(R.id.bg_descending);
        tv_ascending = (TextView) view.findViewById(R.id.tv_ascending);
        tv_descending = (TextView) view.findViewById(R.id.tv_descending);
        et_lower_price = (EditText) view.findViewById(R.id.et_lower_price);
        et_higher_price = (EditText) view.findViewById(R.id.et_higher_price);
        popup = new DatePopup(ProductClassificationActivity.this, view);
        // 设置点击窗口外边窗口消失
        popup.setBackgroundDrawable(new BitmapDrawable());
        popup.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        popup.setFocusable(true);
        //以某个控件的x和y的偏移量位置开始显示窗口
        popup.showAsDropDown(title_layout, 0, 110);
        //如果窗口存在，则更新
        popup.update();
        et_lower_price.setText(lower_price);
        et_higher_price.setText(higher_price);
        if (price_temperature == 0) {
            bg_ascending.setBackground(getResources().getDrawable(R.drawable.fen_border));
            tv_ascending.setTextColor(getResources().getColor(R.color.color_F91D48));
        } else {
            bg_descending.setBackground(getResources().getDrawable(R.drawable.fen_border));
            tv_descending.setTextColor(getResources().getColor(R.color.color_F91D48));
        }

        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                view_pellucidity.setVisibility(View.GONE);
            }
        });
        view_pellucidity.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popup != null) {
                    if (popup.isShowing()) {
                        popup.dismiss();
                        popup = null;
                    }
                }
                view_pellucidity.setVisibility(View.GONE);
                return true;
            }
        });
        view_pellucidity.setVisibility(View.VISIBLE);
        layout_empty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_lower_price.setText("");
                et_higher_price.setText("");
                lower_price = "";
                higher_price = "";
                price_temperature = 0;
                bg_ascending.setBackground(getResources().getDrawable(R.drawable.fen_border));
                tv_ascending.setTextColor(getResources().getColor(R.color.color_F91D48));
                bg_descending.setBackground(getResources().getDrawable(R.drawable.hui_border));
                tv_descending.setTextColor(getResources().getColor(R.color.color_999999));
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lower_price = et_lower_price.getText().toString();
                higher_price = et_higher_price.getText().toString();
                if (lower_price.equals("")) {
                    lower_price = "";
                    lower = 0;
                    higher = 0;
                } else {
                    lower = Integer.parseInt(lower_price);
                }
                if (higher_price.equals("")) {
                    lower_price = "";
                    lower = 0;
                    higher = 0;
                } else {
                    higher = Integer.parseInt(higher_price);
                }
                if (lower > higher) {
                    ToastUtil.showToast("输入错误，最低价不高于最高价");
                } else {
                    setIndexSelected(1);
                    selectRadioButton(1);
                    if (price_temperature == 0) {
                        drawable_price = getResources().getDrawable(R.drawable.icon_top);
                        drawable_price.setBounds(0, 0, 30, 50);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
                        rb_price.setCompoundDrawables(null, null, drawable_price, null);//只放上面
                        bean.setSortType("5");
                        bean.setCategoryId(categoryId);
                        bean.setMinPrice(lower_price);
                        bean.setMaxPrice(higher_price);
                        EventBus.getDefault().post(bean);
                        popup.dismiss();
                    } else {
                        drawable_price = getResources().getDrawable(R.drawable.icon_bottom);
                        drawable_price.setBounds(0, 0, 30, 50);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
                        rb_price.setCompoundDrawables(null, null, drawable_price, null);//只放上面
                        bean.setSortType("4");
                        bean.setCategoryId(categoryId);
                        bean.setMinPrice(lower_price);
                        bean.setMaxPrice(higher_price);
                        EventBus.getDefault().post(bean);
                        popup.dismiss();
                    }
                }
                SharePreferencesHelper.getInstance(ProductClassificationActivity.this).putString("sortType", bean.getSortType());
                SharePreferencesHelper.getInstance(ProductClassificationActivity.this).putString("lower_price", lower_price);
                SharePreferencesHelper.getInstance(ProductClassificationActivity.this).putString("higher_price", higher_price);

            }


        });
        bg_ascending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 /*升序*/
                price_temperature = 0;
                bg_ascending.setBackground(getResources().getDrawable(R.drawable.fen_border));
                tv_ascending.setTextColor(getResources().getColor(R.color.color_F91D48));
                bg_descending.setBackground(getResources().getDrawable(R.drawable.hui_border));
                tv_descending.setTextColor(getResources().getColor(R.color.color_999999));
            }
        });
        tv_descending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    /*降序*/
                price_temperature = 1;
                bg_descending.setBackground(getResources().getDrawable(R.drawable.fen_border));
                tv_descending.setTextColor(getResources().getColor(R.color.color_F91D48));
                bg_ascending.setBackground(getResources().getDrawable(R.drawable.hui_border));
                tv_ascending.setTextColor(getResources().getColor(R.color.color_999999));
            }
        });

    }

    /**
     * 设置当前选中的radiobutton的文字颜色
     *
     * @param i
     */
    private void selectRadioButton(int i) {
        for (int j = 0; j < 3; j++) {
            if (j == i) {
                radioButtons.get(j).setTextColor(getResources().getColor(R.color.sjs_text));
                radioButtons.get(j).setChecked(true);
            } else {
                radioButtons.get(j).setTextColor(getResources().getColor(R.color.home_text));
            }
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharePreferencesHelper.getInstance(ProductClassificationActivity.this).putString("productCategoryId", null);
    }
}
